<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- <img src="../assets/heima.png" /> -->
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-right' : 'el-icon-back'"></i>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#2446B4"
          :unique-opened="true"
          :collapse="isCollapse"
          :router="true"
          :default-active="activePath"
        >
          <!-- index 是唯一标识(值为字符串)，区别于多个同级submenu，若多个submen index值一样，则会默认一次点击打开多个submen -->
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 这个 template 是一级菜单的内容模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 在一级菜单中，可以嵌套二级菜单 index值唯一-->
            <!-- 二级子菜单 -->
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/' + subItem.path"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span slot="title">{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main><router-view /></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      // 页面左侧菜单数据
      menuList: null,
      // 页面左侧菜单图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否展开或收缩左侧菜单
      isCollapse: false,
      //
      activePath: ''
    }
  },
  methods: {
    // 退出功能
    logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取页面侧边栏数据
    async getMenuList() {
      const { data: res } = await this.https.get('menus')
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(path) {
      // 存储于 sessionStorage 中，以便防止页面跳转导致连接未被激活
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  },
  // 实例创建后自动调用 created()
  created: function () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<!-- vue单文件组件中, style标签中的scoped可以防止组件之间的样式冲突，没有scoped则样式是全局的 -->
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    span {
      padding-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  transition: all 0.5s ease-out;
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: #4a5064;
  line-height: 30px;
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
</style>
